﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tycho.Peyote {
    /// <summary>
    /// Interaction logic for PlayBox.xaml
    /// </summary>
    public partial class PlayBox : UserControl {
        public event GlyphMovementHandler GlyphMoved;
        public int Column { get; private set; }
        public int Row { get; private set; }
        public Environment Environment { get; set; }

        public Brush GridColor {
            get {
                var checker = Column % 2 + Row % 2;

                switch (checker) {
                    case 0:
                        return Brushes.White;
                    case 1:
                        return Brushes.LightGray;
                    case 2:
                        return Brushes.Gray;
                    default:
                        return Brushes.White;
                }
            }
        }

        public PlayBox (int row, int column, Environment environment) {
            InitializeComponent ();

            DataContext = this;
            Column = column;
            Row = row;
            Environment = environment;
        }

        public bool IsEmpty {
            get {
                return Box.Children.Count == 0;
            }
        }

        public void PlaceGlyph (GlyphHandle glyphHandle, GlyphHandle nextTo) {
            Box.Children.Insert (Box.Children.IndexOf (nextTo) + 1, glyphHandle);

            if (GlyphMoved != null) {
                GlyphMoved (this, new GlyphMovementEvent (glyphHandle));
            }
        }

        public void AddGlyph (GlyphHandle glyphHandle) {
            Box.Children.Add (glyphHandle);

            if (GlyphMoved != null) {
                GlyphMoved (this, new GlyphMovementEvent (glyphHandle));
            }
        }

        public void RemoveGlyph (GlyphHandle glyphHandle) {
            Box.Children.Remove (glyphHandle);

            if (GlyphMoved != null) {
                GlyphMoved (this, new GlyphMovementEvent (glyphHandle));
            }
        }

        private void UserControl_Drop (object sender, DragEventArgs e) {
            System.Diagnostics.Debug.WriteLine ("PlayBox.Drop");
            GlyphHandle glyphHandle = (GlyphHandle) e.Data.GetData (typeof (GlyphHandle));

            Environment.Layout.MoveGlyph (glyphHandle, this);

            e.Handled = true;
        }
    }

    public delegate void GlyphMovementHandler (object sender, GlyphMovementEvent e);

    public class GlyphMovementEvent : EventArgs {
        public GlyphHandle GlyphHandle { get; set; }

        public GlyphMovementEvent (GlyphHandle glyphHandle) {
            GlyphHandle = glyphHandle;
        }
    }
}
